function CSgetState(stateName,chessId){
	var counter = 0;
	var chess = CFindById(chessId);
	var effectList = CEEffectGetter(chessId);
	var parameters = stateName.split("+");
	for(var i in parameters){
		switch(parameters[i]){
			case "可移动":{
				return true;
			}
			case "可攻击":{
				return true;
			}
			case "可施放技能0":{
				if(chess.skill.length>0){
					if(chess.skill[0].cost<=CSgetState("MP",chessId)&&chess.skill[0].type =="initiative"&&CEEffectFind(chessId,"沉默")==null)
						return true;		
				}
				return false;
			}
			case "可施放技能1":{
				if(chess.skill.length>1){
					if(chess.skill[1].cost<=CSgetState("MP",chessId)&&chess.skill[1].type =="initiative"&&CEEffectFind(chessId,"沉默")==null)
						return true;		
				}
				return false;
			}
			case "可施放技能2":{
				if(chess.skill.length>2){
					if(chess.skill[2].cost<=CSgetState("MP",chessId)&&chess.skill[2].type =="initiative"&&CEEffectFind(chessId,"沉默")==null)
						return true;		
				}
				return false;
			}
			case "可施放技能3":{
				if(chess.skill.length>3){
					if(chess.skill[3].cost<=CSgetState("MP",chessId)&&chess.skill[3].type =="initiative"&&CEEffectFind(chessId,"沉默")==null)
						return true;		
				}
				return false;
			}
			case "可施放技能4":{
				if(chess.skill.length>4){
					if(chess.skill[4].cost<=CSgetState("MP",chessId)&&chess.skill[4].type =="initiative"&&CEEffectFind(chessId,"沉默")==null)
						return true;		
				}
				return false;
			}
			case "可施放技能5":{
				if(chess.skill.length>5){
					if(chess.skill[5].cost<=CSgetState("MP",chessId)&&chess.skill[5].type =="initiative"&&CEEffectFind(chessId,"沉默")==null)
						return true;		
				}
				return false;
			}
			case "名称":{
				return chess.chess.name;
			}
			case "称号":{
				return chess.chess.title;
			}
			case "破坏":{
				if(chess.chess.destory != undefined && chess.chess.destory == true){
					return true;
				}
				else{
					return false;
				}
			}
			case "射程":{
				var number = parseInt(chess.chess.range);
				for(var i in effectList){
					var keyword = effectList[i].effect.split(",");
					var para = effectList[i].parameter.split(",");
					for(var j in keyword){
						if(keyword[j] == "射程增益"){
							number += parseInt(para[j]);
						}
						if(keyword[j] == "射程减益"){
							number -= parseInt(para[j]);
						}
					}
				}
				if(number<=0)
					break;
				counter += number;
				break;
			}
			case "速度":{
				counter += parseInt(chess.chess.speed);
				break;
			}
			case "移动力":{
				var number = parseInt(chess.chess.mov);
				for(var i in effectList){
					var keyword = effectList[i].effect.split(",");
					var para = effectList[i].parameter.split(",");
					for(var j in keyword){
						if(keyword[j] == "移动增益"){
							number += parseInt(para[j]);
						}
						if(keyword[j] == "移动减益"){
							number -= parseInt(para[j]);
						}
					}
				}
				if(number<=0)
					break;
				counter += number;
				break;
			}
			case "攻击力":{
				var number = parseInt(chess.chess.atk);
				for(var i in effectList){
					var keyword = effectList[i].effect.split(",");
					var para = effectList[i].parameter.split(",");
					for(var j in keyword){
						if(keyword[j] == "攻击增益"){
							number += parseInt(para[j]);
						}
						if(keyword[j] == "攻击减益"){
							number -= parseInt(para[j]);
						}
					}
				}
				if(number<=0)
					break;
				counter += number;
				break;
			}
			case "HP上限":{
				var number = parseInt(chess.chess.hp);
				for(var i in effectList){
					var keyword = effectList[i].effect.split(",");
					var para = effectList[i].parameter.split(",");
					for(var j in keyword){
						if(keyword[j] == "生命增益"){
							number += parseInt(para[j]);
						}
						if(keyword[j] == "生命减益"){
							number -= parseInt(para[j]);
						}
					}
				}
				if(number<=0)
					break;
				counter += number;
				break;
			}
			case "HP":{
				counter += CSgetState("HP上限",chessId)-parseInt(chess.hit);
				break;
			}
			case "生命上限":{
				var number = parseInt(chess.chess.hp);
				for(var i in effectList){
					var keyword = effectList[i].effect.split(",");
					var para = effectList[i].parameter.split(",");
					for(var j in keyword){
						if(keyword[j] == "生命增益"){
							number += parseInt(para[j]);
						}
						if(keyword[j] == "生命减益"){
							number -= parseInt(para[j]);
						}
					}
				}
				if(number<=0)
					break;
				counter += number;
				break;
			}
			case "生命值":{
				counter += CSgetState("HP上限",chessId)-parseInt(chess.hit);
				break;
			}
			case "伤害":{
				counter += parseInt(chess.hit);
				break;
			}
			case "MP上限":{
				counter += parseInt(chess.chess.mp);
				break;
			}
			case "MP":{
				counter += parseInt(chess.mp);
				break;
			}
			case "魔法上限":{
				counter += parseInt(chess.chess.mp);
				break;
			}
			case "魔法值":{
				counter += parseInt(chess.mp);
				break;
			}
			default:{
				if(isNaN(parameters[i])){
					for(var i in effectList){
						var effects_1 = effectList[i].effect.split(",");
						var parameters_1 = effectList[i].parameter.split(",");
						for(var j in effects_1){
							if(parameters[i] == effects_1[j]){
								counter += parseInt(parameters_1[j]);
							}
						}
					}
				}
				else{
					counter += parseInt(parameters[i]);
				}
				break;
			}
		}
	}
	return counter;
};

// 受到直接攻击
function CSBeHit(to,from){
	chess = CFindByPosition(to);
	var effectList = CEEffectGetter(chess.id);
	// 开始遍历所有效果
	for(var i in effectList){
		var effects = effectList[i].effect.split(",");
		var parameters = effectList[i].parameter.split(",");
		for(var j in effects){
			if(effects[j] == "攻击反馈"){
				var temp = new Array();
				temp.push(CFindById(from).position);
				_GEEventList.push(new GEEvent("效果",new ChessEffect(parameters[j],chess.id,temp)));
			}
			if(effects[j] == "攻击发生"){
				var temp = new Array();
				temp.push(chess.position);
				_GEEventList.push(new GEEvent("效果",new ChessEffect(parameters[j],chess.id,temp)));
			}
			if(effects[j] == "反击"){
				if(SRange(chess.position,CFindById(from).position)<=CSgetState("射程",chess.id)){
					var temp = new Array();
					temp.push(CFindById(from).position);
					_GEEventList.push(new GEEvent("效果",new ChessEffect(parameters[j],chess.id,temp)));
				}
			}
		}
	}
};

/**
 * 计算伤害
 * 
 * @param id
 *            目标id
 * @param damage
 *            伤害值
 * @param from
 *            来源id
 * @returns {boolean} 是否造成实际伤害
 */
function CSDamage(id,i,from){
	var damage = parseInt(i);
	var chess = CFindById(id);
	var effectList = CEEffectGetter(id);
	var maxdamage = 999;
	// 开始遍历所有效果
	for(var i in effectList){
		var effects = effectList[i].effect.split(",");
		var parameters = effectList[i].parameter.split(",");
		for(var j in effects){
			if(effects[j] == "伤害加深"){
				damage += CECounter(parameters[j],from,id);
			}
			if(effects[j] == "伤害减免"){
				damage -= CECounter(parameters[j],from,id);
			}
		}
	}
	for(var i in effectList){
		var effects = effectList[i].effect.split(",");
		var parameters = effectList[i].parameter.split(",");
		for(var j in effects){
			if(effects[j] == "最高伤害"){
				var num = CECounter(parameters[j],from,id);
				if(num < maxdamage)
					maxdamage = num;
			}
		}
	}
	for(var i in effectList){
		var effects = effectList[i].effect.split(",");
		var parameters = effectList[i].parameter.split(",");
		for(var j in effects){
			if(effects[j] == "激怒"){
				var temp = {};
				temp.id = "Wrath";
				temp.name = "激怒"
				temp.effect = "持续,攻击增益";
				temp.parameter = "1,"+damage;
				temp.description = "提高"+damage+"点攻击力";
				chess.effect.push(temp);
			}
		}
	}
	for(var i in effectList){
		var effects = effectList[i].effect.split(",");
		var parameters = effectList[i].parameter.split(",");
		for(var j in effects){
			if(effects[j] == "伤害延缓"){
				var delay = CECounter(parameters[j],from,id);
				if(damage >= delay){
					damage -= delay;
				}
				else{
					delay = damage;
					damage = 0;
				}
				var temp = {};
				temp.id = "Delay";
				temp.name = "伤害延缓"
				temp.effect = "持续,持续移除生命";
				temp.parameter = "1,"+delay;
				temp.description = "延缓"+delay+"点伤害";
				chess.effect.push(temp);
			}
		}
	}
	for(var i in effectList){
		var effects = effectList[i].effect.split(",");
		var parameters = effectList[i].parameter.split(",");
		for(var j in effects){
			if(effects[j] == "受到伤害"){
				if(damage > 0){
					_GEEventList.push(new GEEvent("效果",new ChessEffect(parameters[j],chess.id,TargetToMulti(chess.position))));
				}
			}
		}
	}
	// 处理攻击力
	if(damage > maxdamage)
		damage = maxdamage;
	if(damage<=0)
		return false;
	chess.hit = parseInt(chess.hit) + parseInt(damage);
	GUIRefrshChess();
	return true;
};

function CSDestory(id,from){
	var chess = CFindById(id);
	chess.destory = true;
	return true;
};

function CSRemoveHP(id,damage,from){
	var chess = CFindById(id);
	chess.hit = parseInt(chess.hit) + parseInt(damage);
	GUIRefrshChess();
	return true;
};

function CSHeal(id,heal,from){
	chess = CFindById(id);
	if(chess.hit > parseInt(heal))
		chess.hit = parseInt(chess.hit) - parseInt(heal);
	else
		chess.hit = 0;
	return true;
};

function CSUseMagic(id,cost){
	chess = CFindById(id);
	if(CSgetState("MP", id)>=cost){
		chess.mp -= cost;
		return true;
	}
	else
		return false;
};

function CSRegenerateMana(id,mana){
	chess = CFindById(id);
	if(CSgetState("MP", id)+parseInt(mana) <= CSgetState("MP上限", id)){
		chess.mp = parseInt(chess.mp) + parseInt(mana);
	}
	else{
		chess.mp = CSgetState("MP上限", id);
	}
};

function CSDestoryListener(){
	var temp = new Array();
	for(var i in _GDChessList){
		if(CSgetState("伤害",_GDChessList[i].id)>=CSgetState("HP上限",_GDChessList[i].id)){
			temp.push(_GDChessList[i].id);
		}else if(CSgetState("破坏",_GDChessList[i].id)){
			temp.push(_GDChessList[i].id);
		}
	}
	if(temp != ""){
		GUIHideNowAction();
		_GEEventList.push(new GEEvent("破坏",temp));
	}
};

function CSDestory(id){
	var effectList = CEEffectGetter(id);
	var deadbody = false;
	for(var i in effectList){
		var effects = effectList[i].effect.split(",");
		var parameters = effectList[i].parameter.split(",");
		for(var j in effects){
			if(effects[j] == "遗言"){
				var temp = new Array();
				temp.push(CFindById(id).position);
				_GEEventList.push(new GEEvent("效果",new ChessEffect(parameters[j],effectList[i].from,temp)));
			}
			if(effects[j] == "驱役" && !deadbody){
				var temp = new Array();
				temp.push(CFindById(id).position);
				_GEEventList.push(new GEEvent("效果",new ChessEffect(parameters[j],effectList[i].from,temp)));
				deadbody = true;
			}
		}
	}
	CFindById(id).sprite.removeAllChildren(true);
	CFindById(id).sprite.removeFromParent(true);
	for(var i in _GDThisTurnNoMoveList){
		if(_GDThisTurnNoMoveList[i].id == id){
			_GDThisTurnNoMoveList.splice(i,1);
			break;
		}
	}
	for(var i in _GDThisTurnDelayList){
		if(_GDThisTurnDelayList[i].id == id){
			_GDThisTurnDelayList.splice(i,1);
			break;
		}
	}
	for(var i in _GDChessList){
		if(_GDChessList[i].id == id){
			_board[_GDChessList[i].position.x][_GDChessList[i].position.y] = null;
			_GDChessList.splice(i,1);
			break;
		}
	}
	if(_GDNowChess != null && id == _GDNowChess.id){
		_GDNowChess = null;
	}
	GLWinListener();
};